package com.poi;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @author xiang
 * @create 2021/8/25 16:45
 */
public class PoiUtil {
    /**
     * 二层list转成excel
     * @author xiang
     * @date 16:46 2021/8/25
     * @param  * @param
     * @return void
     **/
    public static void writeList(List<List<String>> data, OutputStream outputStream) throws IOException {

        // 创建工作薄
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 创建工作表
        XSSFSheet sheet = workbook.createSheet("demo");
        int rowIndex = 0,line=0;
        for (List<String> datum : data) {
            XSSFRow row = sheet.createRow(rowIndex);
            for (String s : datum) {
                XSSFCell cell = row.createCell(line++);
                cell.setCellValue(s);
            }
            line=0;
            rowIndex++;
        }
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        workbook.close();
        System.out.println("导出成功！");
    }
}
